"""
Case Type   : 共享存储-系统内部工具-dsscmd
Case Name   : dsscmd工具显示帮助
Create At   : 2023/4/11
Owner       : opentestcase019
Description :
    1.dsscmd工具正常显示帮助
    2.dsscmd工具使用异常参数-H，--h, -help, --HELP等
Expect      :
    1.成功
    2.合理报错，但也会显示帮助信息
History     :
"""
import os
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common


class ShareStorageTool(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.primary_node = Node('PrimaryDbUser')
        self.common = Common()
        self.expect = """Usage:dsscmd [command] [OPTIONS]

Usage:dsscmd -h/--help show help information of dsscmd
Usage:dsscmd -a/--all show all help information of dsscmd
Usage:dsscmd -v/--version show version information of dsscmd
commands:

Usage:dsscmd cv <-g vg_name> <-v vol_name> [-s au_size] [-D DSS_HOME]
[manage command] create volume group

Usage:dsscmd lsvg [-m measure_type] [-t show_type] [-U UDS:socket_domain]
[client command]Show information of volume group and disk usage space

Usage:dsscmd adv <-g vg_name> <-v vol_name> [-f] [-D DSS_HOME] \
[-U UDS:socket_domain]
[client command]add volume in volume group

Usage:dsscmd mkdir <-p path> <-d dir_name> [-U UDS:socket_domain]
[client command]make dir

Usage:dsscmd touch <-p path> [-U UDS:socket_domain]
[client command]create file

Usage:dsscmd ts [-U UDS:socket_domain]
[client command]Show current API invoking time

Usage:dsscmd ls <-p path> [-m measure_type] [-U UDS:socket_domain]
[client command]Show information of volume group and disk usage space

Usage:dsscmd cp <-s src_file> <-d dest_file> [-U UDS:socket_domain]
[client command]copy source file to destination file

Usage:dsscmd rm <-p path> [-U UDS:socket_domain]
[client command]remove device

Usage:dsscmd rmv <-g vg_name> <-v vol_name> [-f] [-D DSS_HOME] \
[-U UDS:socket_domain]
[client command]remove volume of volume group

Usage:dsscmd rmdir <-p path> [-r] [-U UDS:socket_domain path]
[client command] remove dir or with it's contents recursively

Usage:dsscmd inq <-t inq_type> [-D DSS_HOME]
[raid command] inquiry LUN information or reservations

Usage:dsscmd inq_reg <-i inst_id> [-D DSS_HOME]
[raid command]check whether the node is registered

Usage:dsscmd lscli
[client command] Show information of client

Usage:dsscmd kickh <-i inst_id> [-D DSS_HOME]
[client command] kick off the host from the array

Usage:dsscmd reghl [-D DSS_HOME]
[manage command] register host to array

Usage:dsscmd unreghl [-t type] [-D DSS_HOME]
[manage command] unregister host from array

Usage:dsscmd auid <-a auid>
[tool command] show auid

Usage:dsscmd examine <-p path> <-o offset> <-f format> [-s read_size] \
[-D DSS_HOME] [-U UDS:socket_domain]
[client command] display dss file content

Usage:dsscmd dev <-p path> <-o offset> <-f format> 
[client command] display dev file content

Usage:dsscmd showdisk <-g vg_name> <-s struct_name> [-D DSS_HOME]
      dsscmd showdisk <-g vg_name> <-b block_id> <-n node_id> [-D DSS_HOME]
[client command] show disk information

Usage:dsscmd rename <-o old_name> <-n new_name> [-U UDS:socket_domain]
[client command] rename file, all file name must begin with '+'

Usage:dsscmd du <-p path> [-f format] [-U UDS:socket_domain]
[client command] show disk usage of the file/dir with optional params

Usage:dsscmd find <-p path> <-n name> [-U UDS:socket_domain]
[client command]find files by name from path recursively

Usage:dsscmd ln <-s src_path> <-t target_path> [-U UDS:socket_domain]
[client command]make links between files

Usage:dsscmd readlink <-p path> [-U UDS:socket_domain]
[client command]read link path

Usage:dsscmd unlink <-p path> [-U UDS:socket_domain]
[client command] unlink path

Usage:dsscmd encrypt
[client command] password encrypt

Usage:dsscmd setcfg <-n name> <-v value> [-s scope] [-U UDS:socket_domain]
[client command] set config value by name

Usage:dsscmd getcfg <-n name> [-U UDS:socket_domain]
[client command] get config value by name

Usage:dsscmd getstatus [-U UDS:socket_domain]
[client command] get dss server status

Usage:dsscmd stopdss [-U UDS:socket_domain]
[client command] stop dss server

Usage:dsscmd scandisk <-t type> <-p path> <-u user_name> <-g group_name>
[client command] Scan disk to rebuild soft link

Usage:dsscmd clean_vglock [-D DSS_HOME]
[manage command] clean vg lock

Usage:dsscmd repl <-g vg_name> <-o old_vol> <-n new_vol> [-f] [-D DSS_HOME]
[client command]replace old volume to new volume in volume group

Usage:dsscmd rollback <-g vg_name> [-f] [-D DSS_HOME]
[client command]rollback volume group

Usage:dsscmd showmem <-g vg_name> <-s struct_name> [-U UDS:socket_domain]
      dsscmd showmem <-g vg_name> <-b block_id> <-i index_id> \
[-U UDS:socket_domain]
      dsscmd showmem <-g vg_name> <-f fid> <-n node_id> [-o offset -z size] \
[-U UDS:socket_domain]
      dsscmd showmem <-p path> [-o offset -z size] [-U UDS:socket_domain]
[client command] showmem information

Usage:dsscmd fshowmem <-m memory_file_path> <-g vg_name> <-s struct_name> \
[-D DSS_HOME]
      dsscmd fshowmem <-m memory_file_path> <-g vg_name> <-b block_id> \
<-i index_id> [-D DSS_HOME]
      dsscmd fshowmem <-m memory_file_path> <-g vg_name> <-f fid> \
<-n node_id> [-o offset -z size] [-D DSS_HOME]
      dsscmd fshowmem <-m memory_file_path> <-g vg_name> <-p path> \
[-o offset -z size] [-D DSS_HOME]
[client command] fshowmem information

Usage:dsscmd truncate <-p path> <-l length> [-U UDS:socket_domain]
[client command]truncate file to length

Usage:dsscmd dis_grab_lock [-U UDS:socket_domain]
[client command] if the dssserver is primary, will release cm lock to be \
standby and not to grab lock

Usage:dsscmd en_grab_lock [-U UDS:socket_domain]
[client command] set dssserver to have the right to grab lock"""

    def test_sharestorage_tool(self):
        step1 = "-----step1:dsscmd工具正常显示帮助;expect:成功-----"
        self.logger.info(step1)
        cmd_list = ['-h', '--help']
        for cmd in cmd_list:
            execute_cmd = f"source {macro.DB_ENV_PATH};dsscmd {cmd}"
            self.logger.info(execute_cmd)
            execute_msg = self.common.get_sh_result(self.primary_node,
                                                    execute_cmd)
            self.assertIn(self.expect, execute_msg, "执行失败" + step1)

        step2 = "-----step2:dsscmd工具使用异常参数-H，--h, -help, --HELP等;" \
                "expect:合理报错，但也会显示帮助信息-----"
        self.logger.info(step2)
        cmd_list = ['-H', '--h', '-help', '--HELP']
        for cmd in cmd_list:
            execute_cmd = f"source {macro.DB_ENV_PATH};dsscmd {cmd}"
            self.logger.info(execute_cmd)
            execute_msg = self.common.get_sh_result(self.primary_node,
                                                    execute_cmd)
            self.assertIn(f"cmd:{cmd} can not find",
                          execute_msg, "执行失败" + step2)
            self.assertIn(self.expect, execute_msg, "执行失败" + step2)

    def tearDown(self):
        self.logger.info("-----无需环境清理-----")
        self.logger.info(f"-----{os.path.basename(__file__)} end-----")
